package com.neusoft.weixin;


import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neusoft.weixin.service.CoreService;
import com.neusoft.weixin.service.feedback.FeedbackService;
import com.neusoft.weixin.service.person.PersonService;
import com.neusoft.weixin.utils.SignUtil;



@Controller
@RequestMapping("/web")
public class WebController {
	
	
	private Logger log = LoggerFactory.getLogger(WebController.class);
	
	@Autowired
	private CoreService coreService;
	
	@Autowired
	private PersonService personService;
	
	@Autowired
	private FeedbackService feedbackService;
	
	@RequestMapping(value = "")
	public void weixin(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		
		if ( request.getMethod().equals("GET") ){
			log.info("GET请求");
			String signature = request.getParameter("signature");
			String timestamp = request.getParameter("timestamp");
			String nonce = request.getParameter("nonce");
			String echostr = request.getParameter("echostr");
			
			if ( SignUtil.checkSignature(signature, timestamp, nonce)){
				response.getWriter().write(echostr);
			}
			
		}else if ( request.getMethod().equals("POST")){
			log.info("POST请求");
			String respMessage = coreService.processRequest(request);
			if( null != respMessage ){
				PrintWriter out = response.getWriter();
				out.write(respMessage);
				out.close();
			}
            
		}
            

	}
	
	@RequestMapping(value = "/step1")
	public String step1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String openId = request.getParameter("openId");
		request.getSession().setAttribute("openId", openId);
		
		Map<String,String> person = personService.getPersonInfoByOpenId(openId);
		if ( null != person ){
			request.setAttribute("person", person);
			return "web/userbind/checkStatus";
		}
		
		return "web/userbind/step1";
		
		
	}
	
	@RequestMapping(value = "/step2")
	public String step2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String cardId = request.getParameter("cardId");
		Map<String,String> person = personService.getPersonInfo(cardId);
		if ( null == person ){
			person = new HashMap<String, String>();
			person.put("CARDID", cardId);
		}
		request.getSession().setAttribute("person", person);
		
		return "web/userbind/step2";
		
	}
	
	@RequestMapping(value = "/step3")
	public String step3(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		System.out.println(request.getParameterMap());
		String cardId = request.getParameter("CARDID");
		String name = request.getParameter("NAME");
		String sex = request.getParameter("SEX");
		String phone = request.getParameter("PHONE");
		String birthday = request.getParameter("BIRTHDAY");
		String openId = (String) request.getSession().getAttribute("openId");
		if (null == openId ){
			openId = "";
		}
		if ( null == name ){
			name = "";
		}
		if ( null == sex ){
			sex = "";
		}
		
		Map<String,String> userInfo = new HashMap<String, String>();
		userInfo.put("cardId", cardId);
		userInfo.put("name", name);
		userInfo.put("sex", sex);
		userInfo.put("phone", phone);
		userInfo.put("openId", openId);
		userInfo.put("birthday", birthday);
		
		personService.insertUserInfo(userInfo);
		
		return "web/userbind/step3";
		
		
	}
	@RequestMapping(value = "/orderStep1")
	public String orderStep1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String openId = request.getParameter("openId");
		request.getSession().setAttribute("openId", openId);
		request.setAttribute("hosName", "测试医院");
		
		return "web/order/step1";
	}
	
	@RequestMapping(value = "/orderStep2")
	public String orderStep2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String openId = (String) request.getSession().getAttribute("openId");
		System.out.println(openId);
		//根据openId获取绑定信息
		Map<String,String> person = personService.getPersonInfoByOpenId(openId);
		request.getSession().setAttribute("person", person);
		
		return "web/order/step2";
	}
	
	@RequestMapping(value = "/orderStep3")
	public String orderStep3(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//处理患病信息
		
		
		return "web/order/step3";
	}
	
	@RequestMapping(value = "/orderStep4")
	public String orderStep4(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//处理预约信息
		
		return "web/order/step4";
	}
	
	
	@RequestMapping(value = "/checkStatus")
	public String checkStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String openId = request.getParameter("openId");
		
		Map<String,String> person = personService.getPersonInfoByOpenId(openId);
		if ( null != person ){
			request.setAttribute("person", person);
		}
		
		return "web/userbind/checkStatus";
	}
	
	
	@RequestMapping(value = "/service")
	public String service(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		log.info("进行服务评价");
		String openId = request.getParameter("openId");
		//查询最新的挂号信息
		
		Map<String,String> person = personService.getPersonInfoByOpenId(openId);
		request.setAttribute("person", person);
		
		return "web/feedback/service";
	}
	
	@RequestMapping(value = "/feedBack")
	public String feedBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		log.info("进行建议反馈");
		
		String openId = request.getParameter("openId");
		
		Map<String,String> person = personService.getPersonInfoByOpenId(openId);
		if ( null != person ){
			request.setAttribute("person", person);
		}
		
		return "web/feedback/feedback";
	}
	
	@RequestMapping(value = "/saveFeedBack")
	public String saveFeedBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		log.info("保存建议反馈");
		String cardId = request.getParameter("CARDID");
		String name = request.getParameter("NAME");
		String phone = request.getParameter("PHONE");
		String feedBack = request.getParameter("FEEDBACK");
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("NAME", name);
		map.put("CARDID", cardId);
		map.put("PHONE", phone);
		map.put("FEEDBACK", feedBack);
		
		feedbackService.insertFeedback(map);
		
		return "web/feedback/ok";
	}
	
}
